"use strict";

var _Object$keys = require("@babel/runtime-corejs3/core-js-stable/object/keys");

var _Object$getOwnPropertySymbols = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols");

var _filterInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/filter");

var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor");

var _Object$getOwnPropertyDescriptors = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors");

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/toConsumableArray"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));

function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); enumerableOnly && (symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? Object.defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }

/**
 * 树型结构转数组
 *
 * @since 0.0.4
 * @category 树算法
 * @param {array} tree 树型结构数据集合.
 * @param {object} config 配置参数.
 * @param {boolean} config.props 属性配置
 * @param {string} config.props.prop 数据的唯一主键key, 默认: id.
 * @param {string} config.props.parentProp 数据的父主键key, 默认: parentId.
 * @param {string} config.props.childrenProp 数据的子主键key, 默认: children.
 * @param {function} config.mapping 数据转换回调函数.
 *
 * @returns { array } 转换后数据集合
 *
 * @example
 *
 * z.treeToArray([{ id: 1, children: [ { id: 3, parentId: 1 } ] }, { id: 2 }])
 * // => [ { id: 1 }, { id: 3, parentId: 1 }, { id: 2 } ]
 *
 * z.treeToArray([{ code: 1, pCode: '', list: [ { code: 3, pCode: '', parentId: 1 } ] }, { code: 2, pCode: '' }], { props: { prop: 'code', parentProp: 'pCode', parentValue: '', childrenProp: 'list' }})
 * // => [ { code: 1, pCode: '' }, { code: 3, pCode: '', parentId: 1 }, { code: 2, pCode: '' } ]
 *
 */
function treeToArray(treeList) {
  var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
      _ref$props = _ref.props;

  _ref$props = _ref$props === void 0 ? {} : _ref$props;
  var _ref$props$prop = _ref$props.prop,
      prop = _ref$props$prop === void 0 ? 'id' : _ref$props$prop,
      _ref$props$parentProp = _ref$props.parentProp,
      parentProp = _ref$props$parentProp === void 0 ? 'parentId' : _ref$props$parentProp,
      _ref$props$childrenPr = _ref$props.childrenProp,
      childrenProp = _ref$props$childrenPr === void 0 ? 'children' : _ref$props$childrenPr,
      _ref$mapping = _ref.mapping,
      mapping = _ref$mapping === void 0 ? undefined : _ref$mapping;
  var res = [];
  treeList.forEach(function (item) {
    var obj = _objectSpread({}, item);

    res.push(typeof mapping === 'function' ? mapping(obj) : obj);

    if (obj[childrenProp]) {
      res.push.apply(res, (0, _toConsumableArray2.default)(treeToArray(obj[childrenProp], {
        props: {
          prop: prop,
          parentProp: parentProp,
          childrenProp: childrenProp
        },
        mapping: mapping
      })));
      delete obj[childrenProp];
    }
  });
  return res;
}

module.exports = treeToArray;